var init = require("init")
cc.gameStart = cc.gameStart || {
        num: 0,
        Score: 0,
        node: {},
        gameOver: true,
        isStart: false,
        prefab: null,
        notEnd: false,
        startGame: true,
        gameName: window.game_Name,
        sence:{},
        AdTime: new Date().getTime(),
        change(){},
        getPrefabInit: function(bol, game){
            if(this.prefab) return ;
            var self = this;
            cc.loader.loadRes("/prefab/LibStarGame/LibStarGame", cc.Prefab, function (err, prefab) {
                //加入下面這兩行進行判斷
                if (err) {
                    return;
                }
                self.prefab = prefab;
                if(bol) {
                    self.getPrefab(game);
                }
            });
        }, 
        progressInit(){
            this.nodeJs.rangeTimerInit(this.game.progressBg, this.game.progressMain)
        },
        progressStart(num,bol){
            this.nodeJs.rangeTimerStartTime(num, bol);
        },
        progressEnd(){
            return this.nodeJs.rangeTimerEndTime();
        },

        getPrefab: function(game){ // 获取gameStartPrefab
            cc.voicePlay.loader();
            cc.bgm.loader();
            cc.shareBg.loader();
            cc.gameList.loader();
            cc.voicePlay.isStart = true;
            this.standAloneTimerBol = false;
            this.notEnd = false;
            this.game = game;
            cc.sys.garbageCollect();
           
            if(this.prefab !== null){
                this.gameInit(game);
                return ;
            }else{
                this.getPrefabInit(true, game)
            }
        },
        gameInit: function(game){
            this.AdTime = new Date().getTime(),
            this.notEnd = game.notEnd;
            var newNode = cc.instantiate(this.prefab);
            this.nodeJs = newNode.getComponent("LibStartGame_node");
            cc.director.getScene().addChild(newNode);
            this.progressInit();
            this.gameOver = true;
            this.start();
            try{
                if(this.startGame){
                    this.startGame = false;
                    window.DownloadEnd();
                }
            }catch(err){

            }
        },
        
        setScore: function(score){
            if(!this.nodeJs) return ;
            if(score !== this.Score){
                 this.nodeJs.ScoreAnimation();
            }
            if(!this.notEnd){
                if(this.Score >= score){
                    this.EndAnimat()
                }
            }
            if(isNaN(score)) score = this.Score;
            if(isNaN(score)) score = 0;
            if(!this.isStart) return ;
            this.nodeJs.setScore(score);
            this.Score = score;
            if(this.standAloneTimerBol){
                if(this.Score % 10 == 0){
                    this.standAloneTimer = this.standAloneTimer * 9 / 10;
                }
                //this.nodeJs.Stand_AloneTimerSetTime(this.standAloneTimer);
                this.progressStart(this.standAloneTimer)
            }
        },

        start: function(){ //开始游戏时
            this.nodeJs.init(this.game.GamePlayImg);
        },

        gameStart(){
            if(typeof this.game.standAloneTimer == "number" && this.game.standAloneTimer > 0){
                this.standAloneTimer = this.game.standAloneTimer;
                this.standAloneTimerBol = true;
                this.progressStart(this.standAloneTimer)
            }
            this.isStart = true;
            this.game.gameStart();
        },

        EndAnimat: function(bol){
            if(this.isStart){
                this.progressEnd();
                cc.voicePlay.isStart = false;
                cc.find("Canvas").emit("gameover");
                this.voiceControl = setTimeout(()=>{
                    cc.audioEngine.stopAll();
                }, 500)
                this.game.gameOver();
                clearInterval(this.game.timer);
                if(bol){
                    this.nodeJs.timeOverEven(this.Score, this.maxScore());
                }else{
                    this.nodeJs.gameOverEven(this.Score, this.maxScore());
                }
                this.isStart = false;
                this.num ++;
                if(new Date().getTime() - this.AdTime >= (cc.gameList.AdTime || 8) * 1000){
                    window.showInterstitial.show();
                };
            }
        },

        maxScore(){
            if(!cc.playData.maxScore){
                cc.playData.maxScore = 0;
            };
            var max = 0;
            if(this.Score > cc.playData.maxScore){
                max = this.Score;
                cc.sdkbox.setDataAsync({
                    maxScore: max
                });
                cc.sdkbox.setScoreAsync(max);
            }else{
                max = cc.playData.maxScore;
            }
            cc.playData.maxScore = max;
            return max;
        }
    };
cc.gameStart.getPrefabInit();
module.exports = cc.gameStart;